home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Network Support Library
/
RoseWare - Network Support Library.iso
/
apidev
/
os2spx.arc
/
KNUTCODE.OCT
next >
Wrap
Text File
|
1989-07-27
|
5KB
|
177 lines
/*
SPX sample program
*/
#include <stdio.h>
#include <malloc.h>
#include <ipxcalls.h>
#include <spxcalls.h>
#include <spxerror.h>
#define INCL_BASE
#include <os2.h>
#include <nwcalls.h>
#include <doscalls.h>
#define STACK_SIZE 4000 /* Stack space for each thread */
#define PACKET_NUM 50 /* Number of packets to send */
BYTE NetAddress[4]; /* Space for Node and Network address */
BYTE NodeAddress[6]; /* Keep these two together! */
LONG quit_sem_val = 0;
HSEM quit_sem = &quit_sem_val;
main()
{
void far receiver(); /* Address of receiver */
void far sender(); /* Address of sender */
TID thread_ID; /* Thread ID */
WORD far *thread_stack_ptr; /* Pointer to thread's stack */
WORD socket_num; /* Socket number */
BYTE internet_address[10]; /* Net and Node addresses */
printf("SPX test program:\n");
printf(" Send and receive %d packets\n\n", PACKET_NUM);
/* Obtain current network and node addresses */
IpxGetInternetworkAddress(internet_address);
memmove(NetAddress, internet_address, sizeof(NetAddress));
memmove(NodeAddress, &internet_address[4], sizeof(NodeAddress));
/* Open socket for use by both ends of connection */
socket_num = 0;
SpxOpenSocket(&socket_num);
/* Set the semaphore so main knows when to quit */
DosSemSet(quit_sem);
/* Spin off the thread to do receives */
thread_stack_ptr = (int far *) malloc(STACK_SIZE);
thread_stack_ptr += (STACK_SIZE / sizeof(WORD));
*(--thread_stack_ptr) = socket_num;
DosCreateThread(receiver, &thread_ID, (char far *) thread_stack_ptr);
/* Spin off the thread to do sends */
thread_stack_ptr = (int far *) malloc(STACK_SIZE);
thread_stack_ptr += (STACK_SIZE / sizeof(WORD));
*(--thread_stack_ptr) = socket_num;
DosCreateThread(sender, &thread_ID, (char far *) thread_stack_ptr);
/* Wait until the packets are received */
DosSemWait(quit_sem, -1L);
/* Close the socket before exiting */
SpxCloseSocket(socket_num);
printf("*** Done sending packets\n");
/* Exit and kill the child threads */
DosExit(1, 0);
}
void far receiver(WORD socket_num)
{
/* Variables for receiving packets */
SPX_ECB ecb; /* ECB */
BYTE retryCount = 0; /* retry count */
BYTE watchDog = 0; /* watch dog */
WORD conn; /* connection number */
SPX_HEADER spxhdr; /* SPX headers */
LONG sem_val = 0; /* RAM */
HSEM sem = &sem_val; /* semaphore */
int packet_buf; /* packet area */
int i; /* loop count */
/* ECB information for Listen end */
ecb.fragCount = 1;
ecb.fragList[0].fragAddress = &spxhdr;
ecb.fragList[0].fragSize = sizeof(spxhdr);
ecb.reserved1 = (LONG) sem;
/* Set the semaphore, post the listen, and wait */
DosSemSet(sem);
SpxListenForConnection(socket_num, &ecb, retryCount, watchDog, &conn);
DosSemWait(sem, -1L);
printf("*** Receiver connected\n");
printf("*** Begin sending packets\n");
/* Receive packets */
for (i = 0; i < PACKET_NUM; i++) {
/* Clear area to receive the packet */
packet_buf = 0;
/* ECB stuff for Receive end */
ecb.fragCount = 2;
ecb.fragList[0].fragAddress = &spxhdr;
ecb.fragList[0].fragSize = sizeof(spxhdr);
ecb.fragList[1].fragAddress = &packet_buf;
ecb.fragList[1].fragSize = sizeof(packet_buf);
ecb.reserved1 = (LONG) sem;
/* Set the semaphore, post the listen, and wait for packet */
DosSemSet(sem);
SpxListenForSequencedPacket(socket_num, &ecb);
DosSemWait(sem, -1L);
printf(".");
}
/* Receiver done sending packets */
printf("\n");
DosSemClear(quit_sem);
DosExit(0, 0);
}
void far sender(WORD socket_num)
{
SPX_ECB ecb; /* ECB */
BYTE retryCount = 0; /* retry count */
BYTE watchDog = 0; /* watch dog */
WORD conn; /* connection number */
SPX_HEADER spxhdr; /* SPX headers */
HSYSSEM sem; /* System semaphore */
int i;
/* ECB stuff for Establish end */
ecb.fragCount = 1;
ecb.fragList[0].fragAddress = &spxhdr;
ecb.fragList[0].fragSize = sizeof(spxhdr);
DosCreateSem(1, &sem, "\\SEM\\SPXTEST\\EXAMPLE.SPX");
ecb.reserved1 = (LONG) sem;
/* SPX Header stuff */
memmove(&(spxhdr.destNet), NetAddress, sizeof(NetAddress));
memmove(spxhdr.destNode, NodeAddress, sizeof(NodeAddress));
spxhdr.destSocket = socket_num;
/* Set the semaphore, establish, and wait */
DosSemSet(sem);
SpxEstablishConnection(socket_num, &ecb, retryCount, watchDog, &conn);
DosSemWait(sem, 1000L);
printf("*** Sender connected\n");
/* Send packets */
for (i = 0; i < PACKET_NUM; i++) {
/* ECB stuff for Send end */
ecb.fragCount = 2;
ecb.fragList[0].fragAddress = &spxhdr;
ecb.fragList[0].fragSize = sizeof(spxhdr);
ecb.fragList[1].fragAddress = &i;
ecb.fragList[1].fragSize = sizeof(i);
ecb.reserved1 = (LONG) sem;
/* SPX Header stuff for Send end */
memmove(&(spxhdr.destNet), NetAddress, sizeof(NetAddress));
memmove(spxhdr.destNode, NodeAddress, sizeof(NodeAddress));
spxhdr.destSocket = socket_num;
spxhdr.connectionCtl = 0;
spxhdr.dataStreamType = 0;
/* Set the semaphore, send the packet, and wait */
DosSemSet(sem);
SpxSendSequencedPacket(conn, &ecb);
DosSemWait(sem, -1L);
}
/* Sender is done sending packets */
DosExit(0, 0);
}